#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
 xx:   Dialog_userlog dialog;
    if(dialog.exec() == QDialog::Accepted){
        qDebug()<<"accepted";
    }else{
        qDebug()<<"rejected";
        QMessageBox::warning(this,"登录提示","登录失败");
        exit(1);
    }
}

Widget::~Widget()
{
    delete ui;
}




void Widget::on_pushButton_3_clicked()
{
    QString path = QFileDialog::getExistingDirectory(this,"打开图片路径","C:/Users/123/Pictures/Screenshots",QFileDialog::ShowDirsOnly);
    if(!path.isEmpty()){
        QDir dir(path);//创建QDir对象处理目录
        //获取目录霞所有.png文件列表
        this->imgList = dir.entryInfoList(QStringList()<<"*.png"<<"*.jpg"<<"*.jepg"<<"*.bmp"<<"*.gif",QDir::Files);
        if(!imgList.isEmpty()){
            this->imgIndex = 0;
            //显示第一个找到的图片文件
            displayImg();



        }else{
            ui->label->setText("该路径没有图片");
        }
    }

    //ui->label_2->
}

void Widget::on_pushButton_clicked()
{

    if(!this->imgList.isEmpty()){
       imgIndex=(imgIndex-1+imgList.size())%imgList.size();
       displayImg();
    }else{
        ui->label->setText("该路径没有图片");
    }
}
void Widget:: displayImg(){
    if(this->imgIndex >=0&&imgIndex < imgList.size()){
        QFileInfo fileInfo = imgList.at(imgIndex);
        QString imgPath = fileInfo.absoluteFilePath();
        QString filename = fileInfo.fileName();
        QPixmap pixmap(imgPath);
  if(!pixmap.isNull()){
      ui->label_2->setPixmap(pixmap);
      ui->label_2->setScaledContents(true);
      qDebug()<<imgPath+"/"+filename;
      ui->label->setText(imgPath+"/"+filename);
  }else{
      ui->label_2->setText("图片加载失败");
  }
    }
}

void Widget::on_pushButton_2_clicked()
{
    if(!this->imgList.isEmpty()){
       imgIndex=(imgIndex-1+imgList.size())%imgList.size();
       displayImg();

    }else{
        ui->label->setText("该路径没有图片");
    }
}
